<?php

/**
 * 支付相关
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\member\controller;

class Payment extends \app\common\controller\Member
{

    /**
     * 订单支付页
     */
    public function real_order()
    {
        $pay_sn = input('post.pay_sn');
        $payment_code = input('post.payment_code');

        $url = url('member/order/index');

        if (!preg_match('/^\d{18}$/', $pay_sn)) {
            return $this->error('参数错误');
        }

        $logic_payment = model('Common/Payment', 'logic');
        $result = $logic_payment->getPaymentInfo($payment_code);
        if (!$result['status']) {
            return $this->error($result['msg'], $url);
        }

        $payment_info = $result['data'];


        //计算所需支付金额等支付单信息
        $result = $logic_payment->getRealOrderInfo($pay_sn, $this->member_id);
        if (!$result['status']) {
            return $this->error($result['msg'], $url);
        }

        if ($result['data']['api_pay_state'] || empty($result['data']['api_pay_amount'])) {
            return $this->error('该订单不需要支付', $url);
        }

        //转到第三方API支付
        return $this->_api_pay($result['data'], $payment_info);
    }

    /**
     * 第三方在线支付接口
     *
     */
    private function _api_pay($order_info, $payment_info)
    {
        $cls = '\\payment\\'.$payment_info['payment_code'];
        $payment_api = new $cls($payment_info, $order_info);
        if ($payment_info['payment_code'] == 'chinabank') {
            //$payment_api->submit();
        } else if ($payment_info['payment_code'] == 'kqpay') {
            return $payment_api->send_info($payment_info, $order_info);
        } else {
            //@header("Location: " . $payment_api->get_payurl());
        }
        return false;
    }
    
    public function offline()
    {
        $pay_sn = input('post.pay_sn');
        $payment_code = input('post.payment_code');
        $model_order = model('Common/Order');
        $order_pay = $model_order->getOrderPayInfo(array('pay_sn' => $pay_sn, 'buyer_id' => $this->member_id));
        $order = $model_order->getOrderInfo(array('pay_sn' => $pay_sn, 'buyer_id' => $this->member_id));

        if (!preg_match('/^\d{18}$/', $pay_sn)) {
            return $this->error('参数错误');
        }
        if (!$_FILES) {
            return $this->error('请选择凭证再提交');
        } else {
            $data = $this->uploadFile('order_crimg', 'payment');
            if ($data['status']) {
                $order_pay['order_crimg'] = $data['save_path_type'].$data['save_name'];
                $order_data['order_state'] = ORDER_STATE_SUBMIT;
                if ($model_order->editOrderPay($order_pay, array('pay_sn' => $pay_sn)) && $model_order->editOrder($order_data, array('pay_sn' => $pay_sn))) {
                    //返回提交凭证成功页面，等待商户审核后提供相应服务
                    $ok_url = '/?s=member/buy/pay_ok&pay_sn=' . $pay_sn . '&pay_amount=' . ncPriceFormat($order['order_amount']);
                    return redirect($ok_url);
                }
            } else {
                return $this->error($data['error'] ? : '上传失败');
            }
        }
    }

}
